﻿using System;
using System.Activities;
using System.Collections.Generic;
using KPIS.GERP.SERVICE.Model;
using KPIS.GERP.SERVICE.DAL;
using KPIS.GERP.SERVICE.WorkflowService.Class;
using Common = KPIS.GERP.SERVICE.DAL.Common;

namespace KPIS.GERP.SERVICE.WorkflowService.CodeActivity
{
    public class CreateApprovalTaskActivity : System.Activities.CodeActivity
    {
        [RequiredArgument]
        public InArgument<string> Title { get; set; }

        [RequiredArgument]
        public InArgument<int> RefSeq { get; set; }

        [RequiredArgument]
        public InArgument<int> RefTypeSeq { get; set; }

        [RequiredArgument]
        public InArgument<int> Requester { get; set; }

        [RequiredArgument]
        public InArgument<int> Creator { get; set; }

        [RequiredArgument]
        public InArgument<string> ApproveAs { get; set; }

        [RequiredArgument]
        public InArgument<string> ConfirmAs { get; set; }

        [RequiredArgument]
        public OutArgument<approval_task> ApprovalTask { get; set; }

        public OutArgument<string> MessageAfterCreated { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            string inTitle = Title.Get(context);
            string inConfirmAs = ConfirmAs.Get(context);
            string inApproveAs = string.IsNullOrWhiteSpace(ApproveAs.Get(context)) ? "A" : ApproveAs.Get(context);
            int inRefSeq = RefSeq.Get(context);
            int inRefTypeSeq = RefTypeSeq.Get(context);
            int inRequester = Requester.Get(context);
            int inCreator = Creator.Get(context);
            Guid instanceId = context.WorkflowInstanceId;
             
            approval_task newTask = ApprovalTaskUtils.CreateApprovalTask(inTitle, inConfirmAs, inApproveAs, instanceId, inRequester,
                                                                         inCreator, inRefSeq, inRefTypeSeq);

            newTask.InitialAction = inConfirmAs;

            ApprovalTaskUtils.UpdateRefTableRecordStatus(inRefSeq, inRefTypeSeq, inConfirmAs, inRequester);
            
            ApprovalTaskUtils.LoadRequesterEmployee(ref newTask, inRequester, inCreator);

            ApprovalTaskUtils.LoadRuleApproveFromType(ref newTask, inRefTypeSeq);

            {
                ApprovalStorage.Tasks.Add(instanceId, newTask);
                ApprovalStorageDb.AddTask(newTask);
                context.SetValue(ApprovalTask, newTask);
                MessageAfterCreated.Set(context,
                                        "สร้างคำขออนุมัติสำเร็จ ขณะนี้อยู่ในช่วงรออนุมัติตามสายการอนุมัติที่กำหนดไว้");
            }
        }
    }

    public class ChangeStepActivity : System.Activities.CodeActivity
    {
        public InArgument<TaskAction> Actioning { get; set; }
        public InArgument<int> EmployeeSeq { get; set; }
        public InArgument<string> Reasons { get; set; }
        public InArgument<string> MessageTitle { get; set; }
        public InOutArgument<approval_task> ApprovalTask { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            Guid instanceId = context.WorkflowInstanceId;
            TaskAction actioning = Actioning.Get(context);
            approval_task approvalTask = ApprovalTask.Get(context);
            string messageTitle = MessageTitle.Get(context);
            string messageReasons = Reasons.Get(context);
            int actionByEmpSeq = EmployeeSeq.Get(context);
            bool isHeIsTop;

            if (approvalTask.CurrentStep > 1)
                approvalTask.Ready = false;

            // CheckData Before SendMessage
            if (approvalTask.CurrentStep == 0)
            {

            }

            hrm_ps_employees actionByEmp = ApprovalTaskUtils.GetLastEmp(ref approvalTask, actionByEmpSeq);

            var privStatus = approvalTask.Status.ToString();
            approvalTask.TakeAction(actioning, actionByEmp, messageTitle, messageReasons);

            ApprovalTaskUtils.UpdateRefRecordStatus(ref approvalTask, actionByEmpSeq);
            List<int> messagesSeq = new List<int>();

            if (actioning != TaskAction.C && actioning != TaskAction.J && approvalTask.InitialAction != "C" && approvalTask.InitialAction != "D")
            {
                messagesSeq = ApproveDataLoader.LoadNextStep(ref approvalTask, messageTitle, messageReasons, out isHeIsTop);
                ApprovalTaskUtils.CheckApprovalUpper(ref approvalTask, isHeIsTop);
            }
            else if (actioning != TaskAction.J && (actionByEmpSeq == approvalTask.RequestEmpSeq || approvalTask.InitialAction == "C" || approvalTask.InitialAction == "D"))
            {
                approvalTask.Status = TaskStatus.CancelStatus;
                approvalTask.AddHistory("ถูกผู้ร้องขออนุมัติสั่งยกเลิก");
            }
            else
            {
                approvalTask.AddHistoryHead("ถูกคำสั่งให้ " + ApprovalUtil.ConvertShortActionToWord(actioning.ToString()));
            }

            messagesSeq.AddRange(ApprovalTaskUtils.CaseComplete(ref approvalTask, actionByEmp));

            approvalTask.Ready = true;

            context.SetValue(ApprovalTask, approvalTask);

            ApprovalStorage.Tasks[instanceId] = approvalTask;

            ApprovalStorageDb.UpdateTask(approvalTask);

            ApprovalStorageDb.UpdateApprovalTaskHistory(approvalTask.ApprovalTaskSeq,
                                                        actionByEmpSeq,
                                                        approvalTask.Status.ToString(),
                                                        privStatus,
                                                        string.Empty,
                                                        messageTitle,
                                                        messageReasons,
                                                        messagesSeq,
                                                        actioning.ToString()
                );

            // Process data after approved
            if (approvalTask.Status == TaskStatus.Approved)
            {

            }
        }
    }

    public class DeleteWorkflowActivity : System.Activities.CodeActivity
    {
        protected override void Execute(CodeActivityContext context)
        {
            Guid instanceId = context.WorkflowInstanceId;

            ApprovalStorage.Tasks[instanceId].AddHistory("deleting instant " + instanceId + " from static variable");

            ApprovalStorage.Tasks.Remove(instanceId);
        }
    }

    public class ChangeStatusActivity : System.Activities.CodeActivity
    {
        public InArgument<int> EmployeeSeq { get; set; }
        public InOutArgument<approval_task> ApprovalTask { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            Guid instanceId = context.WorkflowInstanceId;

            var approvalTask = ApprovalTask.Get(context);

            context.SetValue<approval_task>(this.ApprovalTask, approvalTask);

            ApprovalStorage.Tasks[instanceId] = approvalTask;

            ApprovalStorageDb.SetStatusTask(approvalTask.ApprovalTaskSeq, approvalTask.Status);
        }
    }
}